package com.appkubes.mall.restful.application.plugin.delivery.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.utils.net.HttpClientUtils;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.core.entity.MallOrderGoods;
import com.appkubes.mall.core.entity.MallOrderPlan;
import com.appkubes.mall.restful.application.order.delivery.entity.dto.AddDeliveryOrderDTO;
import com.appkubes.mall.restful.application.order.delivery.entity.dto.GoodsesDTO;
import com.appkubes.mall.restful.application.order.delivery.entity.dto.OrderPlanDTO;
import com.appkubes.mall.restful.application.order.goods.service.OrderGoodsService;
import com.appkubes.mall.restful.application.order.order.service.OrderPlanService;
import com.appkubes.mall.restful.application.plugin.delivery.service.PluginDeliveryService;
import com.appkubes.mall.restful.application.plugin.delivery.service.PluginDeliveryUrlService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PluginDeliveryServiceImpl implements PluginDeliveryService {

    @Resource
    PluginDeliveryUrlService deliveryUrlService;
    @Resource
    OrderGoodsService orderGoodsService;
    @Resource
    OrderPlanService orderPlanService;


    @Override
    /**
     * 推送订单到配送系统
     */
    public Boolean notifyOrder(MallOrder order) throws Exception {
        System.out.println("开始推送配送订单");
        //解析数据,组装成接口所需格式
        AddDeliveryOrderDTO addDeliveryOrderDTO = this.arrKeyToOrderVo(order);

        String beanString = JSONObject.toJSONString(addDeliveryOrderDTO);
        log.info("notifyOrder: {}=" + beanString);
        //发送请求到配送系统接收订单接口
        Response response = HttpClientUtils.post(deliveryUrlService.getDeliveryOrderAddURL(), beanString);
        String str = response.body().string();
        if (!StringUtils.isEmpty(str)) {
            JSONObject jsonObject = JSON.parseObject(str);
            System.out.println(jsonObject);
            if (jsonObject.getInteger("code") != 200) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 组装数据
     *
     * @param order 订单信息
     * @return AddDeliveryOrderDTO 返回配送所需的实体类
     */
    private AddDeliveryOrderDTO arrKeyToOrderVo(MallOrder order) {
        //组装数据
        //获取订单数据
        AddDeliveryOrderDTO addDeliveryOrderDTO = new AddDeliveryOrderDTO();
        BeanUtils.copyProperties(order, addDeliveryOrderDTO);
        addDeliveryOrderDTO.setUserkey(order.getUserKey());//用户key
        addDeliveryOrderDTO.setUserName(order.getName());//姓名
        addDeliveryOrderDTO.setUserTel(order.getMobile());//电话
        //获取商品信息
        List<GoodsesDTO> goodsList = this.getGoodses(order.getOrderKey());
        addDeliveryOrderDTO.setGoodses(goodsList);
        return addDeliveryOrderDTO;
    }


    /**
     * 根据订单号查询,商品信息,配送计划信息
     * @param orderKey
     * @return
     */
    public List<GoodsesDTO> getGoodses(String orderKey) {
        List<GoodsesDTO> goodses = new ArrayList<>();
        //查询商品表的商品信息
        List<MallOrderGoods>  mallOrderGoodsList= orderGoodsService.getOrderGoodsByOrderKey(orderKey);
        for (MallOrderGoods goods:mallOrderGoodsList) {
            GoodsesDTO goodsesDTO = new GoodsesDTO();
            goodsesDTO.setGoodsKey(goods.getGoodsKey());
            goodsesDTO.setGoodsName(goods.getGoodsName());
            goodsesDTO.setNum(goods.getNum());
            goodsesDTO.setPic(goods.getPic());

            goodsesDTO.setPlans(this.getPlan(goods, orderKey));
            goodses.add(goodsesDTO);
        }
        return goodses;
    }

    /**
     * 获取配送计划list
     *
     * @param goods 商品信息对象
     * @param orderKey     订单key
     * @return OrderPlanDTO list
     */
    public List<OrderPlanDTO> getPlan(MallOrderGoods goods, String orderKey) {
        List<OrderPlanDTO> plans = new ArrayList<>();
        //获取配送计划
        List<MallOrderPlan> planList = orderPlanService.getOrderPlanByOrderKey(orderKey);
        for (MallOrderPlan plan:planList) {
            OrderPlanDTO orderPlanDTO = new OrderPlanDTO();
            orderPlanDTO.setGoodsKey(goods.getGoodsKey());
            orderPlanDTO.setGoodsName(goods.getGoodsName());
            orderPlanDTO.setDeliveryPlanNum(plan.getPlanNum());
            orderPlanDTO.setDeliveryPlanDate(plan.getPlanTime());
            orderPlanDTO.setPic(goods.getPic());
            plans.add(orderPlanDTO);
        }
        return plans;
    }

}
